<template>
  <div class="mai">
    <div class="sha-encryptor">
      <!-- 头部区域 -->
      <div class="header">
        <div class="title-section">
          <div class="logo">
            <svg
              width="32"
              height="32"
              viewBox="0 0 24 24"
              fill="none"
              xmlns="http://www.w3.org/2000/svg"
            >
              <path
                d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z"
                stroke="currentColor"
                stroke-width="2"
              />
              <path d="M8 12H16" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
              <path d="M12 8L12 16" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
            </svg>
          </div>
          <div>
            <h1>SHA 加密工具</h1>
            <p>安全可靠的哈希加密计算</p>
          </div>
        </div>
        <div class="controls">
          <button @click="encryptText" class="encrypt-btn">
            <span class="btn-icon">🔒</span>
            加密
          </button>
          <button @click="copyResult" class="copy-btn">
            <span class="btn-icon">📋</span>
            复制
          </button>
          <button @click="clearAll" class="clear-btn">
            <span class="btn-icon">🗑️</span>
            清空
          </button>
          <button @click="returnFn" class="encrypt-btn">
            <span class="btn-icon">⚡</span>
            返回
          </button>
        </div>
      </div>

      <!-- 算法选择 -->
      <div class="algorithm-section">
        <h3>选择加密算法</h3>
        <div class="algorithm-grid">
          <label
            v-for="algo in algorithms"
            :key="algo.value"
            :class="{ active: algorithm === algo.value }"
            class="algorithm-card"
          >
            <input type="radio" v-model="algorithm" :value="algo.value" class="sr-only" />
            <div class="algorithm-icon">{{ algo.icon }}</div>
            <div class="algorithm-info">
              <span class="algorithm-name">{{ algo.name }}</span>
              <span class="algorithm-desc">{{ algo.desc }}</span>
            </div>
          </label>
        </div>
      </div>

      <!-- 输入区域 -->
      <div class="input-section">
        <div class="section-header">
          <h3>输入文本</h3>
          <span class="char-count">{{ inputText.length }} 字符</span>
        </div>
        <div class="input-container">
          <textarea
            v-model="inputText"
            placeholder="请输入要加密的文本..."
            class="text-input"
            rows="4"
          ></textarea>
          <div class="input-decoration">
            <div class="decoration-line"></div>
            <div class="decoration-line"></div>
            <div class="decoration-line"></div>
          </div>
        </div>
      </div>

      <!-- 结果区域 -->
      <div class="result-section" v-if="hexResult">
        <div class="section-header">
          <h3>加密结果</h3>
          <div class="result-meta">
            <span class="hash-length">{{ hexResult.length }} 位哈希值</span>
          </div>
        </div>

        <div class="result-tabs">
          <button
            v-for="tab in tabs"
            :key="tab.id"
            :class="{ active: activeTab === tab.id }"
            @click="activeTab = tab.id"
            class="tab-btn"
          >
            {{ tab.label }}
          </button>
        </div>

        <div class="result-content">
          <div v-if="activeTab === 'hex'" class="result-display">
            <div class="result-header">
              <span>HEX 格式</span>
              <button @click="copyHex" class="icon-btn" title="复制HEX">
                <span>📄</span>
              </button>
            </div>
            <code class="result-text hex">{{ hexResult }}</code>
          </div>

          <div v-if="activeTab === 'base64'" class="result-display">
            <div class="result-header">
              <span>Base64 格式</span>
              <button @click="copyBase64" class="icon-btn" title="复制Base64">
                <span>📄</span>
              </button>
            </div>
            <code class="result-text base64">{{ base64Result }}</code>
          </div>
        </div>
      </div>

      <!-- 状态提示 -->
      <transition name="slide-down">
        <div v-if="showStatus" :class="['status', statusType]">
          <div class="status-content">
            <span class="status-icon">{{ statusType === 'success' ? '✅' : '❌' }}</span>
            {{ statusMessage }}
          </div>
        </div>
      </transition>

      <!-- 信息面板 -->
      <div class="info-section">
        <div class="info-header">
          <h3>加密信息</h3>
          <div class="info-stats">
            <div class="stat-item">
              <span class="stat-value">{{ encryptionCount }}</span>
              <span class="stat-label">加密次数</span>
            </div>
          </div>
        </div>

        <div class="info-grid">
          <div class="info-card">
            <div class="info-icon">🔐</div>
            <h4>安全加密</h4>
            <p>使用浏览器原生 Web Crypto API，数据不会离开您的设备</p>
          </div>

          <div class="info-card">
            <div class="info-icon">⚡</div>
            <h4>实时计算</h4>
            <p>输入文本即时加密，无需等待，响应迅速</p>
          </div>

          <div class="info-card">
            <div class="info-icon">🎯</div>
            <h4>多算法支持</h4>
            <p>支持 SHA-1、SHA-256、SHA-384、SHA-512 等多种算法</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
// 响应式数据
const inputText = ref('')
const algorithm = ref('SHA-256')
const hexResult = ref('')
const base64Result = ref('')
const showStatus = ref(false)
const statusMessage = ref('')
const statusType = ref('success')
const activeTab = ref('hex')
const encryptionCount = ref(0)
const Router = useRouter()
const returnFn = () => {
  Router.go(-1)
}
// 算法配置
const algorithms = [
  { value: 'SHA-1', name: 'SHA-1', desc: '160位哈希值', icon: '🔑' },
  { value: 'SHA-256', name: 'SHA-256', desc: '256位哈希值', icon: '🔒' },
  { value: 'SHA-384', name: 'SHA-384', desc: '384位哈希值', icon: '🛡️' },
  { value: 'SHA-512', name: 'SHA-512', desc: '512位哈希值', icon: '💎' }
]

// 标签页配置
const tabs = [
  { id: 'hex', label: 'HEX 格式' },
  { id: 'base64', label: 'Base64 格式' }
]

// SHA 加密函数
const encryptText = async () => {
  if (!inputText.value.trim()) {
    showStatusMessage('请输入要加密的文本', 'error')
    hexResult.value = ''
    base64Result.value = ''
    return
  }

  try {
    // 显示加载状态
    hexResult.value = '计算中...'
    base64Result.value = '计算中...'

    const encoder = new TextEncoder()
    const data = encoder.encode(inputText.value)

    const hashBuffer = await crypto.subtle.digest(algorithm.value, data)

    // 转换为 HEX 格式
    const hashArray = Array.from(new Uint8Array(hashBuffer))
    hexResult.value = hashArray.map(b => b.toString(16).padStart(2, '0')).join('')

    // 转换为 Base64 格式
    base64Result.value = btoa(String.fromCharCode(...new Uint8Array(hashBuffer)))

    encryptionCount.value++
    showStatusMessage('加密成功!', 'success')
  } catch (error) {
    console.error('加密错误:', error)
    hexResult.value = ''
    base64Result.value = ''
    showStatusMessage('加密失败: ' + error.message, 'error')
  }
}

// 显示状态消息
const showStatusMessage = (message, type = 'success') => {
  statusMessage.value = message
  statusType.value = type
  showStatus.value = true

  setTimeout(() => {
    showStatus.value = false
  }, 3000)
}

// 复制功能
const copyHex = async () => {
  try {
    await navigator.clipboard.writeText(hexResult.value)
    showStatusMessage('HEX格式已复制!', 'success')
  } catch (err) {
    showStatusMessage('复制失败', 'error')
  }
}

const copyBase64 = async () => {
  try {
    await navigator.clipboard.writeText(base64Result.value)
    showStatusMessage('Base64格式已复制!', 'success')
  } catch (err) {
    showStatusMessage('复制失败', 'error')
  }
}

const copyResult = async () => {
  if (!hexResult.value) {
    showStatusMessage('没有可复制的内容', 'error')
    return
  }
  await copyHex()
}

// 清空所有内容
const clearAll = () => {
  inputText.value = ''
  hexResult.value = ''
  base64Result.value = ''
  showStatusMessage('已清空', 'success')
}

// 监听输入变化，添加防抖
let debounceTimer = null
watch([inputText, algorithm], () => {
  clearTimeout(debounceTimer)
  debounceTimer = setTimeout(() => {
    if (inputText.value.trim()) {
      encryptText()
    } else {
      hexResult.value = ''
      base64Result.value = ''
    }
  }, 300)
})
</script>

<style scoped>
.mai {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.sha-encryptor {
  width: 1300px;
  padding: 20px;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;

  min-height: 100vh;
}

/* 头部样式 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 24px;
  border-radius: 20px;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.title-section {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.title-section h1 {
  margin: 0;
  color: #2d3748;
  font-size: 32px;
  font-weight: 700;
}

.title-section p {
  margin: 4px 0 0 0;
  color: #718096;
  font-size: 18px;
}

.controls {
  display: flex;
  gap: 12px;
}

/* 按钮样式 */
button {
  padding: 12px 20px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
}

.encrypt-btn {
  background: linear-gradient(135deg, #48bb78, #38a169);
  color: white;
}

.encrypt-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(72, 187, 120, 0.4);
}

.copy-btn {
  background: linear-gradient(135deg, #4299e1, #3182ce);
  color: white;
}

.copy-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(66, 153, 225, 0.4);
}

.clear-btn {
  background: linear-gradient(135deg, #f56565, #e53e3e);
  color: white;
}

.clear-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(245, 101, 101, 0.4);
}

.btn-icon {
  font-size: 20px;
}

/* 算法选择 */
.algorithm-section {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 24px;
  border-radius: 20px;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.algorithm-section h3 {
  margin: 0 0 20px 0;
  color: #2d3748;
  font-size: 20px;
  font-weight: 600;
}

.algorithm-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.algorithm-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: white;
}

.algorithm-card:hover {
  border-color: #4299e1;
  transform: translateY(-2px);
}

.algorithm-card.active {
  border-color: #4299e1;
  background: #ebf8ff;
}

.algorithm-icon {
  font-size: 24px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f7fafc;
  border-radius: 8px;
}

.algorithm-info {
  display: flex;
  flex-direction: column;
}

.algorithm-name {
  font-weight: 600;
  color: #2d3748;
}

.algorithm-desc {
  font-size: 16px;
  color: #718096;
}

/* 输入区域 */
.input-section,
.result-section,
.info-section {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 24px;
  border-radius: 20px;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.section-header h3 {
  margin: 0;
  color: #2d3748;
  font-size: 20px;
  font-weight: 600;
}

.char-count,
.hash-length {
  color: #718096;
  font-size: 18px;
  font-weight: 500;
}

.input-container {
  position: relative;
}

.text-input {
  width: 100%;
  padding: 16px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 18px;
  resize: vertical;
  box-sizing: border-box;
  background: white;
  transition: border-color 0.3s ease;
}

.text-input:focus {
  outline: none;
  border-color: #4299e1;
}

.input-decoration {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.decoration-line {
  width: 3px;
  height: 12px;
  background: #cbd5e0;
  border-radius: 2px;
}

/* 结果区域 */
.result-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.tab-btn {
  padding: 8px 16px;
  background: #f7fafc;
  border: 1px solid #e2e8f0;
  color: #718096;
  border-radius: 8px;
  font-size: 18px;
}

.tab-btn.active {
  background: #4299e1;
  color: white;
  border-color: #4299e1;
}

.result-content {
  background: #f7fafc;
  border-radius: 12px;
  overflow: hidden;
}

.result-display {
  padding: 0;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: #edf2f7;
  border-bottom: 1px solid #e2e8f0;
}

.result-text {
  display: block;
  padding: 16px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 18px;
  line-height: 1.6;
  word-break: break-all;
  background: white;
  margin: 0;
}

.result-text.hex {
  color: #2d3748;
}

.result-text.base64 {
  color: #2b6cb0;
}

.icon-btn {
  padding: 8px;
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.3s ease;
}

.icon-btn:hover {
  background: rgba(0, 0, 0, 0.1);
}

/* 状态提示 */
.slide-down-enter-active,
.slide-down-leave-active {
  transition: all 0.3s ease;
}

.slide-down-enter-from,
.slide-down-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

.status {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 1000;
  padding: 16px 20px;
  border-radius: 12px;
  color: white;
  font-weight: 500;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.status.success {
  background: linear-gradient(135deg, #48bb78, #38a169);
}

.status.error {
  background: linear-gradient(135deg, #f56565, #e53e3e);
}

.status-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 信息面板 */
.info-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.info-stats {
  display: flex;
  gap: 20px;
}

.stat-item {
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 24px;
  font-weight: 700;
  color: #4299e1;
}

.stat-label {
  font-size: 16px;
  color: #718096;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.info-card {
  text-align: center;
  padding: 20px;
  background: #f7fafc;
  border-radius: 12px;
}

.info-icon {
  font-size: 32px;
  margin-bottom: 12px;
}

.info-card h4 {
  margin: 0 0 8px 0;
  color: #2d3748;
  font-size: 20px;
}

.info-card p {
  margin: 0;
  color: #718096;
  font-size: 18px;
  line-height: 1.5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sha-encryptor {
    padding: 16px;
  }

  .header {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }

  .title-section {
    flex-direction: column;
    text-align: center;
  }

  .algorithm-grid {
    grid-template-columns: 1fr;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .section-header {
    flex-direction: column;
    gap: 8px;
    align-items: flex-start;
  }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
</style>